package com.woniu.humanresource.controller;

import com.woniu.humanresource.dao.DepartmentsDao;
import com.woniu.humanresource.entity.*;
import com.woniu.humanresource.service.AchievementService;
import com.woniu.humanresource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class AchievementController {

    @Autowired
    AchievementService achievementService;

    @Autowired
    DepartmentsDao departmentsDao;

    @Autowired
    UserService userService;

    @PostMapping("/selectAchievement/{pageIndex}/{pnum}/{dapartid}")
    public PageBean<Achievement> selectAchievement(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pnum") int pnum,
            @PathVariable("dapartid") int dapartid
    ){
        //System.out.println(pageIndex+"==========================");
        List<Achievement> achievements = achievementService.selectAchievement(pageIndex,pnum,dapartid);
        //数据存在pageBean中
        PageBean<Achievement> achievementPageBean = new PageBean<>();
        achievementPageBean.setBeanList(achievements);
        //查询员工总数
        achievementPageBean.setTotalRecord(achievementService.selectAllEmp(pnum,dapartid));
        //查询所有部门
        achievementPageBean.setDepartments(departmentsDao.selectAllDepartment());
        //查询所有的职位
        achievementPageBean.setPositions(userService.selectPositionByCondition(pnum));
        return achievementPageBean;
    }



    @PostMapping("/selectAllAchievementContext")
    public Achievement selectAllAchievementContext(){
        Achievement achievement = achievementService.selectAllAchievementContext();
        return achievement;
    }


    @PostMapping("/updateAchievement/{id}/{uuname}/{pageIndex}/{pnum}/{dapartid}")
    public PageBean<Achievement> updateAchievement(
            @RequestBody Achievement achievement,
            @PathVariable("id") int id,
            @PathVariable("uuname") String uuname,
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pnum") int pnum,
            @PathVariable("dapartid") int dapartid
    ){
        System.out.println(achievement+"============================");
        achievementService.insertAchievement(achievement,id,uuname);
        List<Achievement> achievements = achievementService.selectAchievement(pageIndex,pnum,dapartid);
        //数据存在pageBean中
        PageBean<Achievement> achievementPageBean = new PageBean<>();
        achievementPageBean.setBeanList(achievements);
        //查询员工总数
        achievementPageBean.setTotalRecord(achievementService.selectAllEmp(pnum,dapartid));
        //查询所有部门
        achievementPageBean.setDepartments(departmentsDao.selectAllDepartment());
        //查询所有的职位
        achievementPageBean.setPositions(userService.selectPositionByCondition(pnum));
        return achievementPageBean;
    }
}
